<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <!--<link rel="stylesheet" href="../plugins/elementui/node_modules/element-ui/lib/theme-chalk/index.css">-->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../plugins/elementui/fonts/element-icons.woff"/>
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <!--<script src="../plugins/elementui/node_modules/element-ui/lib/index.js"></script>-->
    <script src="../js/axios-0.18.0.js"></script>

    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>

            <el-table :data.sync="tableData" style="width: 100%" row-key="id" ref="mytable"
                      border lazy :load="load"
                      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                    prop="name"
                    label="菜单名称"
                    width="180"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="linkUrl"
                    label="访问路径"
                    width="180"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="priority"
                    label="路径级别"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="parentMenuId"
                    label="自关联ID"
                    align="center">
                </el-table-column>
                <el-table-column
                    prop="level"
                    label="菜单级别"
                    align="center">
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                    class="pagiantion"
                    @current-change="handleCurrentChange"
                    :current-page="pagination.currentPage"
                    :page-size="pagination.pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="自关联Id" prop="parentMenuId">
                                    <el-select v-model="formData.parentMenuId">
                                        <el-option label="无" value=""></el-option>
                                        <el-option v-for="menu in menuList" :key="menu.id" :label="menu.name" :value="menu.id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单级别" prop="level">
                                    <el-input v-model="formData.level"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="图标" prop="icon" >
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述" prop="description">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="自关联Id" prop="parentMenuId">
                                    <el-select v-model="formData.parentMenuId">
                                        <el-option label="无" value=""></el-option>
                                        <el-option v-for="menu in menuList" :key="menu.id" :label="menu.name" :value="menu.id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单级别" prop="level">
                                    <el-input v-model="formData.level"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="图标" prop="icon">
                                    <el-input v-model="formData.icon"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述" prop="description">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


        </div>
    </div>
</div>
</div>

<script>
    var vm = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize:10,
                total:0,
                queryString:null,
            },
            menuList:[],
            tableData: [],
            loadNodeMap:new Map(),
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
                // linkUrl: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
                path: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
                priority: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
                // parentMenuId: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
                level: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
            }
        },
        created() {
            this.findPage();
        },
        methods:{
            load(tree, treeNode, resolve) {
                // const id = tree.id;
                // this.loadNodeMap.set(id ,{tree, treeNode, resolve});
                //tree，每行的树对象，
                //treeNode，树节点
                setTimeout(() => {
                    resolve(
                        //获取树对象的子对象
                        tree.children
                    )
                }, 500)
            },

            //分页查询
             findPage() {
                //封装分页查询条件
                var param = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };
                //发送ajax请求，进程分页查询
                axios.post("/menu/findPage.do",param).then((res) => {
                    this.tableData = res.data.rows;
                    this.pagination.total = res.data.total;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //动态查询关联Id
            findParentMenuId(){
                axios.get("/menu/findParentMenuId.do").then((res) => {
                    if (res.data.flag){
                        this.menuList = res.data.data;
                    } else {
                        this.$message.error(res.data.message)
                    }
                })
            },

            // 弹出编辑窗口
            handleUpdate(scope) {
                let row = scope.row;
                this.dialogFormVisible4Edit = true;
                //动态查询关联Id
                this.findParentMenuId();
                //回显数据
                axios.get("/menu/findById.do?id=" + row.id).then((res) => {
                    if(res.data.flag){
                        this.formData = res.data.data;//为模型数据赋值，基于VUE数据绑定进行回显
                    }else{
                        this.$message.error(res.data.message);
                    }
                });
            },

            //编辑菜单
             handleEdit() {
                //进行表单校验
                this.$refs['dataEditForm'].validate((valid) => {
                    if(valid){
                        //如果校验通过，发送ajax请求，将表单数据提交到Controller
                        axios.post("/menu/edit.do",this.formData).then((res) => {
                            if(res.data.flag){
                                //处理成功
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                //关闭新增窗口
                                this.dialogFormVisible4Edit = false;
                                //分页查询
                                this.findPage();
                                // window.location.reload();
                                //formData
                                if(this.formData.parentMenuId){
                                    this.resetTableData(this.formData.parentMenuId);
                                }

                            }else{
                                //处理失败
                                this.$message.error(res.data.message);
                            }
                        });
                    }else{
                        //校验不通过，提示错误信息
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                    }
                });
            },

            //添加
            handleAdd () {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if(valid){
                        //如果校验通过，发送ajax请求，将表单数据提交到Controller
                        axios.post("/menu/add.do",this.formData).then((res) => {
                            if(res.data.flag){
                                //处理成功
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                //关闭新增窗口
                                this.dialogFormVisible = false;
                                //分页查询
                                this.findPage();
                                //更新子节点数据
                                if(this.formData.parentMenuId){
                                    this.resetTableData(this.formData.parentMenuId);
                                }

                            }else{
                                //处理失败
                                this.$message.error(res.data.message);
                            }
                        });
                    }else{
                        //校验不通过，提示错误信息
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                    }
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                //动态查询关联Id
                this.findParentMenuId();
                //将添加窗口展示出来
                this.dialogFormVisible = true;
            },
            //删除时错误提示
            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            // 删除菜单
            handleDelete(row) {
                //弹出确认框
                this.$confirm('你确定要删除当前数据吗？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送ajax请求，将id提交到Controller
                    axios.get("/menu/delete.do?id=" + row.id).then((res) => {
                        if(res.data.flag){
                            this.$message({
                                type:'success',
                                message:res.data.message
                            });
                            this.findPage();
                            //更新子节点数据
                            if(row.id){
                                this.resetTableData(this.formData.parentMenuId);
                            }
                        }else{
                            //执行失败
                            this.$message.error(res.data.message);
                        }
                    }).catch((r) => {
                        this.showMessage(r);
                    });
                }).catch(() => {
                    this.$message("已取消");
                });
            },
            resetTableData(id){
                axios.get('/menu/getTreeNodeByParentId.do?pid=' + id).then(res => {
                    if (res.data.flag) {
                        this.$set(this.$refs.mytable.store.states.lazyTreeNodeMap, id, res.data.data);
                    }
                })
            }
        }
    })
</script>


</body>

</html>
